package com.cn.yunhe.rentcars.web.permission;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.mapper.permission.RoleMapper;
import com.cn.yunhe.rentcars.mapper.permission.RoleResourceMpper;
import com.cn.yunhe.rentcars.model.permission.Account;
import com.cn.yunhe.rentcars.model.permission.Role;
import com.cn.yunhe.rentcars.model.permission.RoleResource;

/**
 * 权限管理模块
 * @author 刘源
 *
 */

@RestController
public class RoleController {
	
	@Autowired
	private RoleMapper roleMapper;
	
	@Autowired
	private RoleResourceMpper roleResourceMpper;
	
	/**
	 * 展示角色 信息
	 * @return
	 */
	
	@RequestMapping(value="/roles",method=RequestMethod.GET)
	public ModelAndView showRoles(){
		List<Role> roleList = roleMapper.getRoles();
		Map<String,List<Role>> roleMap = new HashMap<String,List<Role>>();
		roleMap.put("roles",roleList);
		return new ModelAndView("role-list",roleMap);
	}
	
	
	@RequestMapping(value="/addrole",method=RequestMethod.GET)
	public ModelAndView addRole(){
		return new ModelAndView("role-add");
	}
	
	@RequestMapping(value="/role/add",method=RequestMethod.POST)
	public void addRoleInfo(@RequestBody Role role){
		roleMapper.addRole(role);
		//TODO:添加角色和资源相关表
		
		String permisIds = role.getPermisIds();
		String roleId = role.getRoleId();
		
		List<String> permisIdList =  Arrays.asList(permisIds.split(","));

		List<RoleResource> rrList = new ArrayList<RoleResource>();
		for(int i=0;i<permisIdList.size();i++){
			RoleResource rr = new RoleResource();
			rr.setRoleid(roleId);
			rr.setResourceid(permisIdList.get(i));
			rrList.add(rr);
		}
	
		roleResourceMpper.addRoleResources(rrList);
	}
	
	@RequestMapping(value="/isExistRoleName",method=RequestMethod.GET)
	public boolean isExistRoleName(@RequestParam String roleName){
		String[] name=roleName.split(",");
		if(roleMapper.queryByRoleName(name[1])==null){
			return true;
		}else{
			return false;
		}
	}
	
	@RequestMapping(value="/isExistRoleId",method=RequestMethod.GET)
	public boolean isExistRoleId(@RequestParam String roleId){
		String[] id = roleId.split(",");
		if(roleMapper.queryByRoleId(id[1])==null){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 判断验证码是否一致
	 * @param code
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/isEquals", method=RequestMethod.GET)
    public boolean codeIsSame(@RequestParam String code,HttpServletRequest request, HttpServletResponse response){
    	String verifyCode = (String) request.getSession().getAttribute("imageCodeSessionKey"); 
//    	System.out.println("session"+verifyCode);
//    	System.out.println("page"+code);
    	
    	if(verifyCode.equalsIgnoreCase(code)){
    		return true;
    	}else{
    		return false;
    	}
    }
	
	@RequestMapping(value="/delRoles",method=RequestMethod.POST)
	public void delRoles(@RequestParam String roleIds){
		
		//TODU:删除之前判断这个角色下面是否有用户，有提示不开删除。没有则可以。
		List<String> roleIdList = Arrays.asList(roleIds.split(","));
		roleMapper.delRoles(roleIdList);
	}
	
	
	/**
	 * 修改指定的角色信息
	 * @param roleId
	 * @return 修改页面
	 */
	@RequestMapping(value="/editrole",method=RequestMethod.GET)
	public ModelAndView editRole(@RequestParam String roleId){
		Map<String,Object> map = new HashMap<String,Object>();
		Role role = roleMapper.queryByRoleId(roleId);
		map.put("role", role);
		List<RoleResource> resourceIdList = roleResourceMpper.getByRoleId(Integer.parseInt(roleId));
		map.put("ids", resourceIdList);
			System.out.println(resourceIdList.size());
		return new ModelAndView("role-edit",map);
	}
	
	/**
	 * 编辑角色信息保存至数据库
	 * @param role
	 */
	@RequestMapping(value="/role/edit",method=RequestMethod.POST)
	public void editRoleInfo(@RequestBody Role role){
		//TODU:缺少更新角色信息
		roleMapper.updateRoleInfo(role);
		String permisIds = role.getPermisIds();
		String roleId = role.getRoleId();
		List<String> permisIdList =  Arrays.asList(permisIds.split(","));

		List<RoleResource> rrList = new ArrayList<RoleResource>();
		for(int i=0;i<permisIdList.size();i++){
			RoleResource rr = new RoleResource();
			rr.setRoleid(roleId);
			rr.setResourceid(permisIdList.get(i));
			rrList.add(rr);
		}
		roleResourceMpper.delRoleResource(roleId);
		roleResourceMpper.addRoleResources(rrList);
	}
	
	@RequestMapping(value="/setRoleState", method=RequestMethod.POST)
	public void handleRoleState(@RequestParam String roleId,@RequestParam  int isabled){ 
		System.out.println(roleId);
		System.out.println(isabled);
		Role role = new Role();
		role.setRoleId(roleId);
		role.setIsabled(isabled);
		roleMapper.setRoleState(role);
	}
	
	/**
	 * 根据用户角儿编号返回角色名称
	 * @param roleId
	 * @return   roleName to home.ftl
	 */
	@RequestMapping(value="/getRoleNameByRoleId", method=RequestMethod.GET)
	public String getRoleNameByRoleId(@RequestParam String roleId){
		Role role = roleMapper.queryByRoleId(roleId);
		return role.getRoleName();
	}

}
